var util = require('../../utils/util.js');
var apiObject = require('../../utils/api.js');
var app = getApp();


Page({
  data: {
  },

  onShow: function(e) {



    wx.request({
      url: apiObject.api.test,
      method: "post",
      header: {
        'X-token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res)
        res = res.data;
        if (res.code == 200) {
          wx.switchTab({
            url: '/pages/home/main/index',
          })
        }
      }
    })
  },
  phoneInput: function(e) {
    var value = e.detail.value
    this.setData({
      phone: value
    })

    if (this.data.phone.length > 0) {
      this.setData({
        isShowClearIcon: true
      })
    } else {
      this.setData({
        isShowClearIcon: false
      })
    }
    
  },
  passwordInput: function (e) {
    var value = e.detail.value
    this.setData({
      password: value
    })

  },
  clearPhone: function() {
    this.setData({
      phone: "",
      isShowClearIcon: false
    })

  },

  getUserInfo() { //获取用户信息
    var that = this;
    wx.getUserInfo({
      lang: "zh_CN",
      success: function(e) {
        if (app.globalData.userInfo == null) {
          wx.setStorageSync("userInfo", e.userInfo)
        }
      }
    })
    that.sendCode();
  },

  submit: function () {
    var that=this;
    if (this.data.phone.trim() == "") {
      util.showModal("登录帐号不能为空");
      return;
    }
    if (this.data.password.trim() == "") {
      util.showModal("密码不能为空");
      return;
    }
    wx.request({
      url: apiObject.api.login,
      data: {
        loginId: that.data.phone,
        password:that.data.password
      }, 
      header: {
        'Content-Type': "application/x-www-form-urlencoded",
      },
      success: function (res) {
        console.log(res)
        res = res.data;
        if (res.code == 200) {
          wx.setStorageSync("token", res.result.token)
          wx.switchTab({
            url: '../home/main/index',
          })
        } else {
          util.showModal(res.message);
        }
      }
    })


  },
  a: function () {
    wx.scanCode({
      success: function (e) {
        console.log(e)
      }
    })
  }

})